<?php
/**
 * FileName: default.php
 * Date: 24/02/2010
 * License: GNU General Public License
 * Script Version #: 1.0.0
 * JOS Version #: 1.5.x
 * Development Aleksey Pakholkov, Andrey Kvasnevskiy - OrdaSoft(http://ordasoft.com)
 */

	defined('_JEXEC') or die('Restricted access');
	if(!is_file(JPATH_SITE."/components/com_seftranslate/languages.conf.php"))
	{
		echo "<div>Please install SEF translate component first!</div>";
		return;
	}
	$lang_list=parse_ini_file(JPATH_SITE."/components/com_seftranslate/languages.ini");
	$keys = array_keys( $lang_list );
	//print_r($_SERVER);
	$document =& JFactory::getDocument();
	$settings=Array();
	
	require(JPATH_SITE."/components/com_seftranslate/languages.conf.php");
// load jQuery, if not loaded before
if (!JFactory::getApplication()->get('jquery')) {
  JFactory::getApplication()->set('jquery', true);
  // add jQuery
?>   
<script type="text/javascript" src="<?php echo JURI::base().'modules/mod_seftranslate/js/jquery.js'; ?>"></script> 
 <?php
}
?>   
<script type="text/javascript"> 
   var $jqST = jQuery.noConflict(); 
 </script>

 <script  type="text/javascript" src="<?php echo JURI::base(); ?>modules/mod_seftranslate/js/jquery.translate.js">
</script>
<script  type="text/javascript" src="<?php echo JURI::base(); ?>modules/mod_seftranslate/js/cookiesef.js"> </script>
	 <script type="text/javascript">
		if($jqST.cookiesef('seflang') && $jqST.cookiesef('seflang') != '<?php echo $settings['site_language']; ?>') 
      {
  <?php 
        $translator =  $settings['translator'] ;
        switch($translator){
          case 'bing' : 
            if (!empty($settings['appId'])) {
              $appIds = explode(";",$settings['appId'] );
            ?>
              $jqST.translate.load('<?php echo trim($appIds[rand(0,count($appIds)-1)]); ?>' );
              $jqST(function($){$jqST('body').translate('<?php echo $settings['site_language']; ?>', $jqST.cookiesef('seflang'), {toggle:true});});
            <?php 
            }
            break;
          case 'gtranslate' : 
            if (!empty($settings['api_translate'])) {
            ?>
              $jqST.translate.load('<?php echo trim($settings['api_translate']); ?>' );
              $jqST(function($){$jqST('body').translate('<?php echo $settings['site_language']; ?>', $jqST.cookiesef('seflang'), {toggle:true});});
            <?php 
            } 
            
            break;

            default : 
            echo "alert('Please set translate api key')";
            break;
        }      
        ?>
  }
      
    	</script>
	<script >
		if(top.location!=self.location)
					top.location=self.location;
        				window['_tipoff']=function(){};
					window['_tipon']=function(a){};
		 function get_url(name) {
			name=name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
			var regexS="[\\?&]"+name+"=([^&#]*)";
			var regex=new RegExp(regexS);
			
			var results=regex.exec(location.href);
			
			if(results==null)return '';
			return results[1];
		}
		<?php if($trans_metod=='q'){ ?>

			if($jqST.cookiesef('seflang') && $jqST.cookiesef('seflang') != '<?php echo $settings['site_language']; ?>') 
      {
  <?php 
        $translator =  $settings['translator'] ;
        switch($translator){
          case 'bing' : 
            if (!empty($settings['appId'])) {
              $appIds = explode(";",$settings['appId'] );
            ?>
              $jqST.translate.load('<?php echo trim($appIds[rand(0,count($appIds)-1)]); ?>' );
              $jqST(function($){$jqST('body').translate('<?php echo $settings['site_language']; ?>', $jqST.cookiesef('seflang'), {toggle:true});});
            <?php 
            } 
            break;
          case 'gtranslate' : 
            if (!empty($settings['api_translate'])) {
            ?>
              $jqST.translate.load('<?php echo trim($settings['api_translate']); ?>' );
              $jqST(function($){$jqST('body').translate('<?php echo $settings['site_language']; ?>', $jqST.cookiesef('seflang'), {toggle:true});});
            <?php 
            }             
            break;

            default : 
            echo "alert('Please set translate api key')";
            break;
        }      
        ?>
  }



		<?php } ?>
		function go_transl(lang)
		{
			
			
			<?php if($trans_metod=='q'){ ?>
        if(lang=='pt')lang='pt-PT';
        //******************  
        if(lang=='zh-CHS')lang='zh-CN';
        if(lang=='zh-CHT')lang='zh-TW';   
        //******************
					$jqST.cookiesef('seflang', lang);
				if(location.hostname!='<?php echo $_SERVER['HTTP_HOST']; ?>')
				{
					location.href=unescape(get_url('u'));
					return;
				}
  <?php 
        $translator =  $settings['translator'] ;
        switch($translator){
          case 'bing' : 
            if (!empty($settings['appId'])) {
              $appIds = explode(";",$settings['appId'] );
            ?>
              //******************
              if(lang=='pt-PT')lang='pt';
              if(lang=='zh-CN')lang='zh-CHS';
              if(lang=='zh-TW')lang='zh-CHT'; 
              $jqST.cookiesef('seflang', lang);      
              //******************
              $jqST.translate.load('<?php echo trim($appIds[rand(0,count($appIds)-1)]); ?>' );
              $jqST(function($){$jqST('body').translate('<?php echo $settings['site_language']; ?>', $jqST.cookiesef('seflang'), {toggle:true});});
            <?php 
            } else { 
            ?>
              alert( "Please set Bing application ID"); 
            <?php 
            };
            break;
          case 'gtranslate' : 
            if (!empty($settings['api_translate'])) {
            ?>
              $jqST.translate.load('<?php echo trim($settings['api_translate']); ?>' );
              $jqST(function($){$jqST('body').translate('<?php echo $settings['site_language']; ?>', $jqST.cookiesef('seflang'), {toggle:true});});
            <?php 
            } else { 
            ?>
              alert( "Please set Google API key"); 
            <?php 
            };
            
            break;

            default : 
            echo "alert('Please set translate api key')";
            break;
        }      
        ?>
			<?php }else{ ?>
				if(lang=='<?php echo $settings['site_language']; ?>' && location.hostname=='<?php echo $_SERVER['HTTP_HOST']; ?>')
						return;
				if(lang=='<?php echo $settings['site_language']; ?>' && location.hostname!='<?php echo $_SERVER['HTTP_HOST']; ?>')
				{
					
					location.href=unescape(get_url('u'));
					return;
				}
				if(lang!='<?php echo $settings['site_language']; ?>' && location.hostname=='<?php echo $_SERVER['HTTP_HOST']; ?>' )
				{
					location.href='http://translate.google.com/translate?client=tmpg&hl=<?php echo $settings['site_language']; ?>&langpair=<?php echo $settings['site_language']; ?>|'+lang+'&u='+escape(location.href);
					return;
				}
				location.href='http://translate.google.com/translate?client=tmpg&hl=<?php echo $settings['site_language']; ?>&langpair=<?php echo $settings['site_language']; ?>|'+lang+'&u='+escape(unescape(get_url('u')));
			<?php } ?>

		}
		
	</script>
	<div class="languages">
	<form action="index.php" method="post" name="translateForm" id="translateForm" >

	<?php	
	$show_flag_lang=($show_flag_lang=="")?Array():is_array($show_flag_lang)?$show_flag_lang:Array($show_flag_lang);
	foreach( $show_flag_lang as $flag )
	{
		$alt=ucfirst(strtolower(array_search($flag,$lang_list)));
		echo ' <a href="javascript:go_transl(\''.$flag.'\')" ><img src="'.JURI::base().'/modules/mod_seftranslate/tmpl/png/'.$flag.'.png" title="'.$alt.'"  alt="'.$alt.'"/></a>';

	} 
	if((count($show_flag_lang)>1)and($show_lang!=""))
	{
		echo "<br>";
	
	}
?>
	
	<?php
	//$show_flag_lang=($show_flag_lang=="")?Array():is_array($show_flag_lang)?$show_flag_lang:Array($show_flag_lang);
	if($show_lang=="")
	{
// 		foreach( $keys as $key )
// 		{
// 			
// 			
// 			echo '<option value="'.$lang_list[$key].'">'.ucfirst(strtolower($key)).'</option>';
// 			
// 		}
	}
	else
	{
		?>
		<select onchange="go_transl(this.value);">
			<option value="">Select language</option>
		<?php
		if(!is_array($show_lang)){$show_lang=Array($show_lang);}
		foreach( $keys as $key )
		{
			if(array_search($lang_list[$key],$show_lang)!==false)
			{
				echo '<option value="'.$lang_list[$key].'">'.ucfirst(strtolower($key)).'</option>';
			}
			
		}
		?>
		</select>
		<?php
	}
	?>
	</form>
	</div>
<?php /*?><div><a href="http://ordasoft.com" style="font-size: 10px;">Powered by OrdaSoft!</a></div><?php */?>
  
<?php 
?>